package com.meteor.backController;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

@Controller
public class FileController {

    /**
     * 文件上传到本地
     */
    @PostMapping("/upLoad")
    public String upLoad(HttpServletRequest request, Model model) {
        try {
            //拿到文件信息
            Part myFile = request.getPart("myFile");
            //拿到文件名
            String fileName = myFile.getSubmittedFileName();
            File path = new File(ResourceUtils.getURL("classpath:").getPath());
            if(!path.exists()) path = new File("");
            String filePath = path.getAbsolutePath() + "\\static\\images\\" + fileName;
            File upload = new File(path.getAbsolutePath(),"static/images/");
            if(!upload.exists()) upload.mkdirs();

            // D:\MyProject\blogs\target\classes\static\images\touxiang.jpg
           // /blogs/target/classes/static/images/touxiang.jpg
            String path1 = "/images/" + fileName;
            model.addAttribute("filePath", path1);
            myFile.write(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
        return "admin/blogSend";
    }

    /**
     * 文件下载到本地
     */
    @GetMapping("/download")
    public String download(Model model,HttpServletResponse response,HttpSession session) {
        FileInputStream fileInputStream = null;
        ServletOutputStream out = null;
        String filePath = (String) session.getAttribute("filePath");
        try {
            if(filePath != null) {
                fileInputStream = new FileInputStream(new File(filePath));
            out = response.getOutputStream();
            response.setContentType("image/jpeg");
            int len = 0;
            byte[] bytes = new byte[10240000];
            while((len = fileInputStream.read(bytes)) != -1) {
                out.write(bytes,0,len);
                out.flush();
            }
            model.addAttribute("filePath",filePath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "admin/blogSend";
    }

    /**
     * 跳转到kiki页面
     */
    @GetMapping("/kiki")
    public String kiki() {
        return "kiki";
    }


}

